using System;
using System.Collections.Generic;
using System.Web.Mvc;
using AutoMapper;
using SapaFinance.DTO;
using SapaFinance.Domain.Model;
using SapaFinance.GenericRepository;

namespace SapaFinance.Services
{
    public interface IAccountService
    {
        ICorporationService CorporationService { get; set; }
        void CreateAccount(AccountDTO accountDTO);
        void DeleteAccount(Guid id);
        void UpdateAccount(AccountDTO accountDTO);
        IEnumerable<SelectListItem> GetAccountSelectedList(Guid id);
        IEnumerable<AccountDTO> GetAccountList();
       // CurrencyDetailDTO GetCurrencyDetailByAccountId(Guid id);
        Account GetAccount(Guid guid);
    }

    public class AccountService : IAccountService
    {
        private readonly IGenericRepository<Account, Guid> _accountRepository;
        private readonly IEntityFactory _entityFactory;
        private readonly IMappingEngine _mappingEngine;
        private readonly IUnitOfWork _unitOfWork;

        public AccountService(IMappingEngine mappingEngine, IGenericRepository<Account, Guid> accountRepository,
                              IUnitOfWork unitOfWork, IEntityFactory entityFactory)
        {
            _mappingEngine = mappingEngine;
            _accountRepository = accountRepository;
            _unitOfWork = unitOfWork;
            _entityFactory = entityFactory;
        }

        #region IAccountService Members

        public ICorporationService CorporationService { get; set; }
        public IUserService UserService { get; set; }

        public IEnumerable<SelectListItem> GetAccountSelectedList(Guid id)
        {
            var account = _accountRepository.GetById(id);
            return _mappingEngine.Map<Account, IEnumerable<SelectListItem>>(account);
        }

        public void CreateAccount(AccountDTO accountDTO)
        {
            var corporation = CorporationService.GetCorporation(accountDTO.SelectedCorporation);
            var account = _entityFactory.BuildAccount(accountDTO);
            corporation.AddAccount(account);
            CorporationService.Update(corporation);
        }

        public void DeleteAccount(Guid id)
        {
            var account = _accountRepository.GetById(id);
            _accountRepository.Delete(account);
            _unitOfWork.Flush();
        }

        public void UpdateAccount(AccountDTO accountDTO)
        {
            var corporation = CorporationService.GetCorporation(accountDTO.SelectedCorporation);
            if (corporation == null) return;
            var account = corporation.GetAccount(accountDTO.UID);
            var user = UserService.GetUser(accountDTO.CurrentUser);
            if (account == null || user == null) return;
            account.Delete(accountDTO.IsHidden);
            account.SetPrimary(accountDTO.IsPrimaryAccount);
            account.SetOwner(UserService.GetUser(accountDTO.SelectedOwner));
            account.Change(user);
            corporation.ChangeAccount(account);
            CorporationService.Update(corporation);
        }

        //public CurrencyDetailDTO GetCurrencyDetailByAccountId(Guid id)
        //{
        //    var account = _accountRepository.GetById(id);
        //    var currencyDetailDTO = _mappingEngine.Map<Account, CurrencyDetailDTO>(account);
        //    return currencyDetailDTO;
        //}

        public Account GetAccount(Guid guid)
        {
            return _accountRepository.GetById(guid);
        }

        public IEnumerable<AccountDTO> GetAccountList()
        {
            return _mappingEngine.Map<IEnumerable<Account>, IEnumerable<AccountDTO>>(_accountRepository.GetAll());
        }

        #endregion
    }
}